package net.maku.utils;

import java.util.Map;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import net.maku.bean.model.MyException;
import net.maku.constant.SessionConstant;
 
@Slf4j
public class TokenUtils {

    /**
     * 生成 AccessToken
     */
    public static String generator() {
        return UUID.fastUUID().toString(true);
    }

    /**
     * 获取 AccessToken
     */
    public static String getAccessToken(HttpServletRequest request) {
    	String token = ReqUtils.getHeader( request, SessionConstant.AUTHENTICATION, "" );
        if (StrUtil.isBlank(token)) {
        	token = request.getParameter("access_token");
        }
        if (StrUtil.isBlank(token)) {
        	throw new MyException("用户没有传递token");
        }
        //System.out.println("token: " + token);
        log.info("token: " + token);
        //log.info("token: " + token);
        return token;
    }
    
    
    public static String getAccessToken() {
    	ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            return getAccessToken( request );
        }
        throw new MyException("用户没有传递token");
    }
    
    public static Map<String,Object> getUserInfo() {
    	Claims user = NewJwtUtils.parseToken(getAccessToken());
    	System.out.print(user);
        return user;
    }
}


